<template>
  <div class="login-box">
    <van-nav-bar title="用户登录" />
    <van-form @submit="onSubmit" :style="{ 'margin-top': '20px' }">
      <van-cell-group inset>
        <van-field
          v-model="loginInfo.userName"
          name="用户名"
          label="用户名"
          placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
          v-model="loginInfo.passWord"
          type="password"
          name="密码"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
      </van-cell-group>
      <div style="margin: 16px">
        <van-button round block type="primary" native-type="submit">
          提交
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<script setup>
import { showToast } from "vant";
import { reactive, getCurrentInstance } from "vue";
import { useRouter } from "vue-router";

const { proxy } = getCurrentInstance();

const loginInfo = reactive({
  userName: "",
  passWord: "",
});

const router = useRouter();

const onSubmit = async () => {
  const res = await proxy.$api.login(loginInfo).then();
  if (res) {
    localStorage.setItem('h5_token', res.token)
    localStorage.setItem('h5_userInfo', JSON.stringify(res.userInfo))
    showToast("登录成功");
    
    router.push("/");
  }
};
</script>

<style lang="less" scoped>
.login-box {
  height: 100%;
  background-color: #f3f3f3;
}
</style>
